# Author: HZT
# Date: 2023/10/7
import unittest
from config.config_loader import config
from page.web_ck_top import WebCkTop
from utils import UtilsDriver, read_yaml, login_with_session, get_db_ye, get_cid_by_username
from page.web_ck_zl import WebCkZl
from utils import DBUtil
import datetime
import calendar


class TestWebCkZl(unittest.TestCase):
    credentials = None
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = UtilsDriver.get_web_driver()
        # 实例化
        cls.ckZl = WebCkZl(cls.driver)
        cls.ckTop = WebCkTop(cls.driver)
        # 打开网页
        cls.driver.get(config.URL)
        # 登录
        login_with_session('system1', cls.driver)

    @classmethod
    def tearDownClass(cls):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 公司名字显示正确
    def test01_ck_zl(self):
        # 获取公司名称-页面
        gsmc = self.ckZl.ck_zl_get_gsmz()
        # 获取公司名称
        gsmc_1 = self.ckTop.ck_top_get_gsmc_js().split()[0]
        # 断言
        self.assertIn(gsmc_1, gsmc)

    # 余额显示正确
    def test02_ck_zl(self):
        # 获取余额
        ye = self.ckZl.ck_zl_get_ye()
        # 获取数据库余额
        db_ye = get_db_ye(config.CREDENTIALS["username_ck"])
        # 断言
        self.assertEqual(ye.replace(',', ''), db_ye)

    # 今日利润
    def test03_ck_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_ck"])
        # 页面获取本日收卡总额
        d1 = self.ckZl.ck_zl_get_d3()
        if d1 == '￥0.00':
            d1 = 'None'
        # 获取当前日期
        today = datetime.date.today()
        # 构造起始日期和结束日期
        start_date = today.strftime('%Y-%m-%d')
        end_date = (today + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        sql_query = f"""
            SELECT
        (
            SELECT
                IFNULL(sum(rmb_amount), 0)
            FROM
                transactions
            WHERE
                owner_type = 'AdminUser'
            AND STATUS = 1
            AND type = 1
            AND owner_id IN (314)
            AND created_at BETWEEN '{start_date}' AND '{end_date}'
        ) - (
            SELECT
                IFNULL(sum(rmb_amount), 0)
            FROM
                transactions
            WHERE
                owner_type = 'AdminUser'
            AND STATUS = 1
            AND type = 2
            AND owner_id IN (314)
            AND created_at BETWEEN '{start_date}' AND '{end_date}'
	    );
        """
        # 数据库获取本日利润
        d1_d = DBUtil.select_one(sql_query)
        self.assertEqual(d1.replace(',', '').replace("￥", ""), str(d1_d[0]))

    # 本月利润
    def test04_ck_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_ck"])
        # 页面获取本月收卡总额
        d2 = self.ckZl.ck_zl_get_d4()
        # 获取当前日期
        current_date = datetime.date.today()
        # 获取当前月份的起始日期和结束日期
        start_date = current_date.replace(day=1)
        # 获取当前月份的最后一天
        _, last_day = calendar.monthrange(current_date.year, current_date.month)
        end_date = current_date.replace(day=last_day)

        # 拼接起始日期和结束日期的字符串形式
        start_date_str = start_date.strftime('%Y-%m-%d')
        end_date_str = end_date.strftime('%Y-%m-%d')
        sql_query = f"""
                    SELECT
                (
                    SELECT
                        IFNULL(sum(rmb_amount), 0)
                    FROM
                        transactions
                    WHERE
                        owner_type = 'AdminUser'
                    AND STATUS = 1
                    AND type = 1
                    AND owner_id IN (314)
                    AND created_at BETWEEN '{start_date_str}' AND '{end_date_str}'
                ) - (
                    SELECT
                        IFNULL(sum(rmb_amount), 0)
                    FROM
                        transactions
                    WHERE
                        owner_type = 'AdminUser'
                    AND STATUS = 1
                    AND type = 2
                    AND owner_id IN (314)
                    AND created_at BETWEEN '{start_date_str}' AND '{end_date_str}'
        	    );
                """
        # 数据库本月利润
        d2_d = DBUtil.select_one(sql_query)
        self.assertEqual(d2.replace(',', '').replace("￥", ""), str(d2_d[0]))
